package com.sinven.common;

import com.sinven.util.UploadUtil;
import org.apache.tomcat.util.http.fileupload.FileUploadBase;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import javax.servlet.http.HttpServletRequest;

/**
 * @author <a href="mailto:jianfengzhou11@qq.com">Mr Zhou</a>
 * @date 2020/4/28
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    @ResponseBody
    public Object FileSizeLimitExceededHandler(HttpServletRequest
                                          request, MaxUploadSizeExceededException e){
        String method = request.getMethod();
        String path = request.getRequestURI();

        //注意：SpringBoot 404错误默认并没有抛出异常，如果需要使用统一异常处理404错误，需要
//                在SpringBoot中配置没有找到Handle时抛出异常
//        4. 配置SimpleMappingExceptionResolver处理异常
//        3.5 自定义HandlerExceptionResolver 类处理异常
        return UploadUtil.error("Error....上传文件大小超出");
    }
}

